#include "EventLoop.h"
#include "Log.h"
#include "LoopThread.h"
#include <LoopThreadPool.h>
#include <mutex>


namespace muduo{
    LoopThreadPool::LoopThreadPool(EventLoop* baseloop):_base_loop(baseloop),_thread_num(0),_next_index(0){}
    void LoopThreadPool::Set_thread_num(int num){_thread_num=num;}
    void LoopThreadPool::Create(){
        if(_thread_num>0){
            // 一个线程对应一个loop，所以创建多少个线程就有多少个loop
            _thread_pool.resize(_thread_num);
            _loops.resize(_thread_num);
            for(int i=0;i<_thread_num;i++){
                _thread_pool[i]=new LoopThread();
                _loops[i] = _thread_pool[i]->GetEventLoop();
            }
        }
    }
    EventLoop* LoopThreadPool::nextEventLoop(){
        if(_thread_num == 0){
            return _base_loop;
        }
        _next_index=(_next_index+1)%_thread_num;
        return _loops[_next_index];
    }
}